#!/bin/sh

if test "x$RING_TEST_UNINSTALLED" = x; then
  script_fullname=`readlink -e "@ringtestsdir@/twisted/run-test.sh"`
  if [ `readlink -e "$0"` != "$script_fullname" ] ; then
    echo "This script is meant to be installed at $script_fullname" >&2
    exit 1
  fi

  test_src="@ringtestsdir@"
  test_build="@ringtestsdir@"
  config_file="@ringtestsdir@/twisted/tools/servicedir/tmp-session-bus.conf"

  PYTHONPATH="@ringtestsdir@/twisted"
  export PYTHONPATH

  RING_TWISTED_PATH="@ringtestsdir@/twisted"
  export RING_TWISTED_PATH
else
  if test -z "$RING_ABS_TOP_SRCDIR"; then
    echo "RING_ABS_TOP_SRCDIR must be set" >&2
    exit 1
  fi
  if test -z "$RING_ABS_TOP_BUILDDIR"; then
    echo "RING_ABS_TOP_BUILDDIR must be set" >&2
    exit 1
  fi

  test_src="${RING_ABS_TOP_SRCDIR}/tests"
  test_build="${RING_ABS_TOP_BUILDDIR}/tests"
  config_file="${test_build}/twisted/tools/tmp-session-bus.conf"

  PYTHONPATH="${test_src}/twisted:${test_build}/twisted"
  export PYTHONPATH

  RING_TWISTED_PATH="${test_src}/twisted"
  export RING_TWISTED_PATH
fi

if [ -n "$1" ] ; then
  list="$1"
else
  list=$(cat "${test_build}"/twisted/ring-twisted-tests.list)
fi

any_failed=0
for i in $list ; do
  echo "Testing $i ..."
  sh "${test_src}/twisted/tools/with-session-bus.sh" \
    ${RING_TEST_SLEEP} \
    --config-file="${config_file}" \
    -- \
    @TEST_PYTHON@ -u "${test_src}/twisted/$i"
  e=$?
  case "$e" in
    (0)
      echo "PASS: $i"
      ;;
    (77)
      echo "SKIP: $i"
      ;;
    (*)
      any_failed=1
      echo "FAIL: $i ($e)"
      ;;
  esac
done

exit $any_failed
